import os 
import sys 
import shelve 

FILE_LIMIT = 64 * 1024 * 1024 
CHUNK_COUNT = 0 
CURRENT_CHUNK = open('chunk','w')

def isDbFile(fileName):
    if(fileName.find(".db") != -1):
        return True
    return False

def saveToChunk(someRec,bytesWritten):
    result = 0
    global CURRENT_CHUNK,CHUNK_COUNT
    if((bytesWritten + len(someRec)) > FILE_LIMIT):
        CURRENT_CHUNK.close()
        CURRENT_CHUNK = open('chunk'+str(CHUNK_COUNT+1),'w')
        CHUNK_COUNT +=1
        print "CHUNK:",CHUNK_COUNT
        result = len(someRec)
    else:
        result = bytesWritten + len(someRec)
    
    CURRENT_CHUNK.write(someRec)
    return result 

if __name__ == "__main__":
    FILES = filter(isDbFile,os.listdir("."))
    bytesWritten = 0
    for eachFile in FILES:
        db = shelve.open(eachFile)
        for eachKey in db.keys():
            rec = eachKey+"\t"+db[eachKey]+"\n"
            bytesWritten = saveToChunk(rec,bytesWritten)
        db.close()
        
